/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.admin.controller.EmployeeManagerRemote;
import com.common.json.EmployeeManagerJson;
import com.entity.Employee;
import com.model.admin.EmployeeModelRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Tuan Long
 */
public class EmployeeModel {
    EmployeeManagerRemote employeeManager;

    EmployeeModelRemote employeeModel;
    

    public EmployeeModel() {
        employeeModel = lookupEmployeeModelRemote();
        employeeManager = lookupEmployeeManagerRemote();
    }

    private EmployeeModelRemote lookupEmployeeModelRemote() {
        try {
            Context c = new InitialContext();
            return (EmployeeModelRemote) c.lookup("java:global/BluePumpkinEJB/EmployeeModel!com.model.admin.EmployeeModelRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    private EmployeeManagerRemote lookupEmployeeManagerRemote() {
        try {
            Context c = new InitialContext();
            return (EmployeeManagerRemote) c.lookup("java:global/BluePumpkinEJB/EmployeeManager!com.admin.controller.EmployeeManagerRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public String listEmployee() {

        String lstEm = employeeModel.listEmployee();
        return lstEm;

    }

    public String listSearchEmployee(Object key) {

        String lstEm = employeeModel.listSearchEmployee(key);
        return lstEm;

    }

    public boolean createEmployee(String emJson) {        
        return employeeModel.createEmployee(emJson);
    }

    public Employee getEmployeeInfoByUsername(String username) {
        Employee em = new Employee();
        // Su dung phuong thuc EJB de lay chuoi Json tra ve
        String json = employeeManager.getEmployeeInfoById(username);        
        // Sau do viet 1 phuong thuc de doc du lieu tu chuoi Json chuyen sang sang object Employee        
        em = EmployeeManagerJson.convertJsonStringToEmployeeObjec(json);        
        return em;
    }

    /**
     * @param  username
     * @param password
     * @return boolean
     */
    public boolean changeEmployeePassword(String username, String password){
        return employeeManager.ChangeEmployeePassword(username, password);
    }
    /**
     * @return  List<Employee>
     **/
    public List<Employee> getAllEmployee(){
        List<Employee> list = new ArrayList<Employee>();
        List<String> listJson = employeeManager.getAllEmployee();
        for (String json : listJson) {            
            Employee employee = EmployeeManagerJson.convertJsonStringToEmployeeObjec(json);
            list.add(employee);
        }        
        return list;
    }

    public List<Employee> getRegisters(int eventId) {
        List<Employee> list = new ArrayList<Employee>();
        List<String> listJson = employeeManager.getRegisters(eventId);
        for (String json : listJson) {            
            Employee employee = EmployeeManagerJson.convertgetRegistersToEmployeeObj(json);
            list.add(employee);
        }        
        return list;
    }
    
}
